<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>公告列表</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="css/oksub.css">
	<script type="text/javascript" src="lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--数据表格-->
	<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="lib/layui/layui.js"></script>
<script>
	layui.use(["element", "jquery", "table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
		let table = layui.table;
		let form = layui.form;
		let laydate = layui.laydate;
		let okLayer = layui.okLayer;
		let okUtils = layui.okUtils;
		let okMock = layui.okMock;
		let $ = layui.jquery;

		laydate.render({elem: "#startTime", type: "datetime"});
		laydate.render({elem: "#endTime", type: "datetime"});
		okLoading.close($);
		let userTable = table.render({
			elem: '#tableId',
			url: 'api/anno?op=annoHistory',
			limit: 20,
			page: true,
			toolbar: true,
			toolbar: "#toolbarTpl",
			size: "sm",
			cols: [[
				{field: "anno_id", title: "公告ID", width: 100, sort: true},
				{field: "title", title: "标题", width: 200},
				{field: "if_required", title: "是否必读", width: 80,templet: function(d) {
					if(d.if_required==1){
						return "是";
					}else{
						return "否";
					}
				}
				},
				{field: "sender", title: "公告发出人", width: 100},
				{field: "send_time", title: "公告发出时间", width: 120},
				{field: "content", title: "公告内容", width: 200},
				{field: "file_name", title: "附件文件名", width: 185},	
				{field: "attachment", title: "附件url", width: 300, templet: "#urlTpl"},
			]],
			done: function (res, curr, count) {
				console.info(res, curr, count);
			}
		});
		
		form.on("submit(search)", function (data) {
			userTable.reload({
				where: data.field,
				page: {curr: 1}
			});
			return false;
		});

		table.on("toolbar(tableFilter)", function (obj) {
			switch (obj.event) {
				case "batchDel":
					batchDel();
					break;

			}
		});

		table.on("tool(tableFilter)", function (obj) {
			let data = obj.data;
			switch (obj.event) {

				case "del":
					del(data.anno_id);
					break;
			}
		});

		function batchDel() {
			okLayer.confirm("确定要批量删除吗？", function (index) {
				layer.close(index);
				
				//拼接idsstr字段，使其符合json格式
	            var checkStatus = table.checkStatus("tableId");
	            var rows = checkStatus.data.length;
	            var idsStr = "";
	            if (rows > 0) {
	                for (var i = 0; i < checkStatus.data.length; i++) {
	                    idsStr += checkStatus.data[i].anno_id + ",";
	                }
	            } 
				
				if (idsStr) {
					okUtils.ajax("api/anno?op=annoDelete", "post", {idsStr: idsStr}, true).done(function (response) {
						console.log(response);
						okUtils.tableSuccessMsg(response.msg);
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}

		function del(anno_id) {
			okLayer.confirm("确定要删除吗？", function () {
				okUtils.ajax("api/anno?op=annoDelete", "post", {idsStr: anno_id}, true).done(function (response) {
					console.log(response);
					okUtils.tableSuccessMsg(response.msg);
				}).fail(function (error) {
					console.log(error)
				});
			})
		}
	})
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
	<!--模糊搜索区域-->
	<div class="layui-row">
		<form class="layui-form layui-col-md12 ok-search">
			<input class="layui-input" placeholder="公告标题" autocomplete="off" name="title">
			<input class="layui-input" placeholder="公告发出人" autocomplete="off" name="sender">
			<button class="layui-btn" lay-submit="" lay-filter="search">
				<i class="layui-icon">&#xe615;</i>
			</button>
		</form>
	</div>
</script>
<!-- 绑定文件下载链接 -->
<script type="text/html" id="urlTpl">
	<a href="{{d.attachment}}" target="_blank">{{d.attachment}}</a>
</script>
</body>
</html>
